package com.hardroid.workload.ui.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.hardroid.workload.R;
import com.hardroid.workload.utils.HardroidUtils;
import com.hardroid.workload.utils.ITallySelected;
import com.hardroid.workload.workcalc.Tally;

public class TallyItemView extends LinearLayout implements
		OnCheckedChangeListener {

	private int no;
	private Tally tally;
	private TextView noText;
	private TextView obsvTimeText;
	private EditText descText;
	private RadioButton pRadioButton;
	private RadioButton npRadioButton;
	private RadioButton uRadioButton;

	private Context context;
	private ITallySelected iTallySelected;

	public TallyItemView(Context context, Tally tally, int no,
			ITallySelected iTally) {
		super(context);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.iTallySelected = iTally;

		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		inflater.inflate(R.layout.list_item_tally, this, true);

		noText = (TextView) findViewById(R.id.list_item_tally_number);
		obsvTimeText = (TextView) findViewById(R.id.list_item_tally_obsv_time);
		descText = (EditText) findViewById(R.id.list_item_tally_desc);
		pRadioButton = (RadioButton) findViewById(R.id.list_item_tally_status_productive);
		npRadioButton = (RadioButton) findViewById(R.id.list_item_tally_status_non_productive);
		uRadioButton = (RadioButton) findViewById(R.id.list_item_tally_status_unknown);
		descText.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
		

		pRadioButton.setOnCheckedChangeListener(this);
		npRadioButton.setOnCheckedChangeListener(this);
		uRadioButton.setOnCheckedChangeListener(this);

		setTally(tally, no);
	}

	public TallyItemView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public void setTally(Tally tally, int no) {
		Log.i("Set Tally", tally.description);
		this.no = no;
		this.tally = tally;

		noText.setText((no+1) + "");
		obsvTimeText.setText(HardroidUtils.dateToTime(tally.obsvTime));
		
		pRadioButton.setChecked(tally.productive);
		npRadioButton.setChecked(tally.nProductive);
		uRadioButton.setChecked(tally.unknown);

	}

	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub

		if (buttonView == pRadioButton) {
			if (isChecked) {
				tally.productive = true;
			} else {
				tally.productive = false;
			}
		} else if (buttonView == npRadioButton) {
			if (isChecked) {
				tally.nProductive = true;
			} else {
				tally.nProductive = false;
			}
		} else if (buttonView == uRadioButton) {
			if (isChecked) {
				tally.unknown = true;
			} else {
				tally.unknown = false;
			}
		}

		iTallySelected.onTallySelected();

		// if (isChecked)
		// Toast.makeText(context, tally.getTallyStatus(),
		// Toast.LENGTH_SHORT).show();
	}

	
	private Tally getTally(){
		return tally;
	}
	
	private Context getThisContext(){
		return context;
	}
}
